package com.wanglibao.woodtrade.woodtrade.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.model.WoodTypeDetail;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter.HomeFragmentRecommendAdapter;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter.ShopFragmentWoodTypeAdapter;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter.model.Commodity;
import com.wanglibao.woodtrade.woodtrade.util.ToastUtil;
import com.wanglibao.woodtrade.woodtrade.widget.PullToRefreshView.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 供需信息
 * Created by lugg on 2016/5/10.
 */
public class ShopFragment extends BaseFragment {

    @Bind(R.id.tv_fragment_shop_filter)
    TextView tv_fragment_shop_filter;

    @Bind(R.id.edit_fragment_shop_search)
    EditText edit_fragment_shop_search;

    @Bind(R.id.tv_fragment_shop_search_btn)
    TextView tv_fragment_shop_search_btn;

    @Bind(R.id.tv_fragment_shop_sales_sort)
    TextView tv_fragment_shop_sales_sort;

    @Bind(R.id.tv_fragment_shop_price_sort)
    TextView tv_fragment_shop_price_sort;

    @Bind(R.id.list_fragment_shop_shoplist)
    PullToRefreshListView list_fragment_shop_shoplist;

    @Bind(R.id.ll_fragment_shop_filter)
    LinearLayout ll_fragment_shop_filter;

    @Bind(R.id.list_fragment_shop_woodtype)
    ListView list_fragment_shop_woodtype;

    @Bind(R.id.list_fragment_shop_woodTypeDetail)
    ListView list_fragment_shop_woodTypeDetail;

    TextView lastSelectedWoodType,LastSelectedWoodTypeDetail;

    List<String> woodTypeDetailStrings = new ArrayList<>();

    List<WoodTypeDetail> woodTypeDetails = new ArrayList<>();
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_shop, container, false);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        addViewClicklistener(tv_fragment_shop_filter, tv_fragment_shop_search_btn,
                tv_fragment_shop_sales_sort, tv_fragment_shop_price_sort);

        HomeFragmentRecommendAdapter homeFragmentRecommendAdapter = new HomeFragmentRecommendAdapter(this.getContext(),testCommpdity());
        list_fragment_shop_shoplist.setAdapter(homeFragmentRecommendAdapter);

        list_fragment_shop_woodtype.setAdapter(new ShopFragmentWoodTypeAdapter(getContext(),myApplication.woodTypeStringLists));
        list_fragment_shop_woodtype.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (lastSelectedWoodType != null) {
                    lastSelectedWoodType.setBackgroundColor(getResources().getColor(R.color.default_color));
                }
                lastSelectedWoodType = (TextView) view;
                lastSelectedWoodType.setBackgroundColor(getResources().getColor(R.color.white));
                String woodTypeId = myApplication.woodTypeList.get(position).id;
                woodTypeDetails.clear();
                woodTypeDetailStrings.clear();
                for (int i = 0; i < myApplication.woodTypeDetailList.size(); i++){
                    if (woodTypeId.equals(myApplication.woodTypeDetailList.get(i).parent_id)){
                        woodTypeDetails.add(myApplication.woodTypeDetailList.get(i));
                        woodTypeDetailStrings.add(myApplication.woodTypeDetailList.get(i).name);
                    }
                }
                list_fragment_shop_woodTypeDetail.setAdapter(new ShopFragmentWoodTypeAdapter(getContext(),woodTypeDetailStrings));
            }
        });
        list_fragment_shop_woodTypeDetail.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (LastSelectedWoodTypeDetail != null) {
                    LastSelectedWoodTypeDetail.setBackgroundColor(getResources().getColor(R.color.white));
                }
                LastSelectedWoodTypeDetail = (TextView) view;
                LastSelectedWoodTypeDetail.setBackgroundColor(getResources().getColor(R.color.default_color));
                ll_fragment_shop_filter.setVisibility(View.GONE);
                ToastUtil.createNormalToast(getContext(),"搜索"+woodTypeDetails.get(position).name);
            }
        });
    }

    private List<Commodity> testCommpdity() {
        List<Commodity> commodityList = new ArrayList<>();

        for (int i = 0; i < 10; i++){
            Commodity commodity = new Commodity();
            commodity.company = "单位"+i;
            commodity.name = "商品名称"+i;
            commodity.price = "价格"+i;
            commodity.specifications = "规格"+i;
            commodity.url = "http://imgtest.meiliworks.com/pic/_o/55/3a/c5f334a8468fda3b1faeb446b08f_640_960.c1.jpg?frm=out_pic";
            commodityList.add(commodity);
        }

        return commodityList;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.tv_fragment_shop_filter:
                int state = ll_fragment_shop_filter.getVisibility();
                if (state == View.GONE){
                    ll_fragment_shop_filter.setVisibility(View.VISIBLE);
                }else{
                    ll_fragment_shop_filter.setVisibility(View.GONE);
                }
                break;
            case R.id.tv_fragment_shop_search_btn:
                Editable searchText = edit_fragment_shop_search.getText();
                if (!TextUtils.isEmpty(searchText)){
                    ToastUtil.createNormalToast(getActivity(),"调用查询的接口");
                    ll_fragment_shop_filter.setVisibility(View.GONE);
                }else {
                    ToastUtil.createNormalToast(getActivity(),"请输入关键字");
                }
                break;
            case R.id.tv_fragment_shop_sales_sort:
                ToastUtil.createNormalToast(getActivity(),"按销量排序");
                break;
            case R.id.tv_fragment_shop_price_sort:
                ToastUtil.createNormalToast(getActivity(),"按价格排序");
                break;
        }
    }

    void search(String searchText){

    }

    @Override
    public void setMenuVisibility(boolean menuVisible) {
        super.setMenuVisibility(menuVisible);
        if (this.getView() != null)
            this.getView().setVisibility(menuVisible ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
